{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "adjustable-answer",
   "metadata": {},
   "source": [
    "# NumPy索引"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "crude-operations",
   "metadata": {},
   "source": [
    "## 导入NumPy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "superb-allen",
   "metadata": {},
   "outputs": [],
   "source": [
    "##keep\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "african-adams",
   "metadata": {},
   "source": [
    "## 创建数组\n",
    "数组如下图所示：\n",
    "![arr](images/arr.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fifth-breeding",
   "metadata": {},
   "outputs": [],
   "source": [
    "##keep\n",
    "a = np.array([1, 2, 3, 4, 5, 6, 7, 6, 5, 4, 3, 2, 1])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e122de59",
   "metadata": {},
   "source": [
    "## 切片索引"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5242eadb",
   "metadata": {},
   "source": [
    "选出数组前半部分的偶数位的元素："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "eeffc752",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "b40a3303",
   "metadata": {},
   "source": [
    "选出所有偶数下标的元素："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6a6679cd",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "administrative-great",
   "metadata": {},
   "source": [
    "## 布尔索引"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bdabeb3a",
   "metadata": {},
   "source": [
    "下面的布尔表达式的结果是什么？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3008e3e5",
   "metadata": {},
   "outputs": [],
   "source": [
    "##keep\n",
    "a > 5"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "54f0d16d",
   "metadata": {},
   "source": [
    "找出数组a中所有大于5的元素。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "medieval-child",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "29169da6",
   "metadata": {},
   "source": [
    "写出等价的布尔值做索引的表达式："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fef431fb",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "c81a0cac",
   "metadata": {},
   "source": [
    "## 更复杂的布尔索引"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "95ec3e7f",
   "metadata": {},
   "source": [
    "取出a的值在3和5之间的所有元素："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "500e3a4d",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "b3ab6b88",
   "metadata": {},
   "source": [
    "上面表达式中索引值的结果是什么？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "993f5219",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "ab92710c",
   "metadata": {},
   "source": [
    "## 整数数组索引"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "53a32b75",
   "metadata": {},
   "source": [
    "用整数数组索引取出\n",
    "```\n",
    "[2, 3, 4]\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1079cc08",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "401d5a22",
   "metadata": {},
   "source": [
    "使用np.arange表达式作为索引，取出\n",
    "```\n",
    "[5, 6, 7, 6, 5]\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a8229ffd",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "123a0b22",
   "metadata": {},
   "source": [
    "用整数数组索引取出\n",
    "```\n",
    "[2, 2, 3, 2]\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0c75754e",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "d1e5af80",
   "metadata": {},
   "source": [
    "## 索引赋值"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "julian-truck",
   "metadata": {},
   "source": [
    "将数组a中所有3和5之间（包括3和5）的值设置为0。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "polar-arbitration",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.11"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
